/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.AdjustBillBean;
import com.minierp.bean.AdjustBillItemBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.AdjustBillDao;
import com.minierp.dao.AdjustBillItemDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.FinanceDetailDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.PayBillDao;
import com.minierp.dao.ReceiveBillDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.DataChangedException;
import com.minierp.err.NotAllowException;
import com.minierp.helper.PageHelper;
import com.minierp.model.AdjustBill;
import com.minierp.model.AdjustBillItem;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import com.minierp.model.BusinessProfit;
import com.minierp.model.CooperBill;
import com.minierp.model.CooperBillExt;
import com.minierp.model.FinanceDetail;
import com.minierp.model.PurchaseBill;
import com.minierp.model.PurchaseBillExt;
import com.minierp.model.SystemLog;
import com.minierp.service.AdjustBillService;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class AdjustBillServiceImpl implements AdjustBillService {
	public void txInsert(AdjustBillBean form, AdjustBillDao adjustBillDao,
			AdjustBillItemDao adjustBillItemDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator,
			ReceiveBillDao receiveBillDao, PayBillDao payBillDao,
			BusinessBillDao businessBillDao) throws Exception {
		AdjustBill obj = form.getAdjustBill();
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(AdjustBill.class.getName()));
		if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
					.getCodeRule(), "", form.getAdjustBill().getTallySetId()));

			for (AdjustBill updateRelBillCost = adjustBillDao.get(obj
					.getBillCode()); updateRelBillCost != null; updateRelBillCost = adjustBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
						.getCodeRule(), "", form.getAdjustBill()
						.getTallySetId()));
			}
		}

		boolean updateRelBillCost1 = false;
		if (obj.getExamined().equals("Y")) {
			obj.setExamSms("N");
			obj.setExamineTime(new Date());
			updateRelBillCost1 = true;
		} else {
			obj.setExamSms("Y");
		}

		adjustBillDao.save(obj);
		this.saveItems(form, updateRelBillCost1, adjustBillItemDao,
				receiveBillDao, payBillDao, adjustBillDao, businessBillDao,
				idGenerator);
		if (obj.getExamined().equals("Y")) {
			this.insertFinanceDetail(obj, adjustBillDao, idGenerator);
		}

	}

	public void txUpdate(AdjustBillBean form, AdjustBillDao adjustBillDao,
			AdjustBillItemDao adjustBillItemDao, IdGenerator idGenerator,
			ReceiveBillDao receiveBillDao, PayBillDao payBillDao,
			BusinessBillDao businessBillDao) throws Exception {
		AdjustBill obj = form.getAdjustBill();
		Object[] sv = adjustBillDao.getStatusValues(obj.getId());
		if (sv == null) {
			throw new Exception("Can\'t get status values for adjust bill:"
					+ obj.getId());
		} else {
			long existSycVal = ((Date) sv[0]).getTime();
			String exitEnabled = sv[1].toString();
			String existExamined = sv[2].toString();
			if (existSycVal != form.getSychronizeValue()) {
				throw new DataChangedException("调帐单已被其他操作修改，请关闭页面再打开后重新操作！");
			} else if (exitEnabled.equals("N")) {
				throw new NotAllowException("调帐单已作废，不能修改！");
			} else if (existExamined.equals("Y")) {
				throw new NotAllowException("调帐单已审核，不能修改！");
			} else {
				boolean updateRelBillCost = false;
				if (obj.getExamined().equals("Y")) {
					obj.setExamSms("N");
					obj.setExamineTime(new Date());
					updateRelBillCost = true;
				} else {
					obj.setExamSms("Y");
				}

				obj.setUpdateTime(new Date());
				adjustBillDao.update(obj);
				this.saveItems(form, updateRelBillCost, adjustBillItemDao,
						receiveBillDao, payBillDao, adjustBillDao,
						businessBillDao, idGenerator);
				if (obj.getExamined().equals("Y")) {
					this.insertFinanceDetail(obj, adjustBillDao, idGenerator);
				}

			}
		}
	}

	private void insertFinanceDetail(AdjustBill obj,
			AdjustBillDao adjustBillDao, IdGenerator idGenerator)
			throws NotAllowException {
		BigDecimal remCost;
		String financeType;
		if (obj.getAdjustType().equals("COLLECT_ABATE")) {
			financeType = "R";
			remCost = (new BigDecimal(0)).subtract(obj.getCost());
		} else if (obj.getAdjustType().equals("COLLECT_ADD")) {
			financeType = "R";
			remCost = obj.getCost();
		} else if (obj.getAdjustType().equals("COO_ABATE")) {
			financeType = "P";
			remCost = (new BigDecimal(0)).subtract(obj.getCost());
		} else if (obj.getAdjustType().equals("COO_ADD")) {
			financeType = "P";
			remCost = obj.getCost();
		} else if (obj.getAdjustType().equals("PAY_ABATE")) {
			financeType = "P";
			remCost = (new BigDecimal(0)).subtract(obj.getCost());
		} else if (obj.getAdjustType().equals("PAY_ADD")) {
			financeType = "P";
			remCost = obj.getCost();
		} else if (obj.getAdjustType().equals("PUR_BACK_ABATE")) {
			financeType = "P";
			remCost = obj.getCost();
		} else {
			if (!obj.getAdjustType().equals("PUR_BACK_ADD")) {
				throw new NotAllowException(
						"Adjust type not supported while save finance detail!");
			}

			financeType = "P";
			remCost = (new BigDecimal(0)).subtract(obj.getCost());
		}

		FinanceDetail fd = new FinanceDetail();
		fd.setId(idGenerator.getId(FinanceDetail.class.getName()));
		fd.setBillDate(obj.getAdjustDate());
		fd.setBillId(obj.getId());
		fd.setBillType("ADJUST_BILL");
		fd.setRemCost(remCost);
		fd.setAdjCost(remCost);
		fd.setEstId(obj.getClientId());
		fd.setFinanceType(financeType);
		fd.setTallySetId(obj.getTallySetId());
		adjustBillDao.save(fd);
	}

	private void saveItems(AdjustBillBean form, boolean updateRelBillCost,
			AdjustBillItemDao adjustBillItemDao, ReceiveBillDao receiveBillDao,
			PayBillDao payBillDao, AdjustBillDao adjustBillDao,
			BusinessBillDao businessBillDao, IdGenerator idGenerator)
			throws NotAllowException {
		List items = form.getItems();
		int newNum = 0;

		for (int curItemId = 0; curItemId < items.size(); ++curItemId) {
			AdjustBillItemBean bean = (AdjustBillItemBean) items.get(curItemId);
			if (bean.getStatus().equals("NEW")
					&& bean.getAdjustBillItem().getThisCost()
							.compareTo(new BigDecimal(0)) == 1) {
				++newNum;
			}
		}

		long arg18 = 0L;
		if (newNum > 0) {
			arg18 = idGenerator.getId(AdjustBillItem.class.getName(), newNum);
		}

		for (int i = 0; i < items.size(); ++i) {
			AdjustBillItemBean bean1 = (AdjustBillItemBean) items.get(i);
			if (bean1.getOldId() != 0L || !bean1.getStatus().equals("DELETE")) {
				String[] doActs;
				List iList;
				if (form.getAdjustBill().getRelType().equals("BUSINESS_BILL")) {
					BusinessBill b = (BusinessBill) businessBillDao.get(
							BusinessBill.class, bean1.getAdjustBillItem()
									.getBusinessBillId());
					BusinessBillExt be = (BusinessBillExt) businessBillDao.get(
							BusinessBillExt.class, bean1.getAdjustBillItem()
									.getBusinessBillId());
					if (b == null) {
						throw new NotAllowException("找不到id为"
								+ bean1.getAdjustBillItem().getBusinessBillId()
								+ "的业务单");
					}

					doActs = this.saveItem(form, bean1, adjustBillItemDao,
							arg18);
					if (bean1.getStatus().equals("NEW")
							&& bean1.getAdjustBillItem().getThisCost()
									.compareTo(new BigDecimal(0)) == 1) {
						++arg18;
					}

					if (doActs[0].equals("Y")) {
						if (be.getExamined().equals("N")) {
							throw new NotAllowException("单号为" + b.getBillCode()
									+ "的业务单还没审核，不能操作");
						}

						if (be.getEnabled().equals("N")) {
							throw new NotAllowException("单号为" + b.getBillCode()
									+ "的业务单已作废，不能操作");
						}

						if (be.getClosed().equals("Y")) {
							throw new NotAllowException("单号为" + b.getBillCode()
									+ "的业务单已结清，不能操作");
						}

						if (b.getClientId() != form.getAdjustBill()
								.getClientId()) {
							throw new NotAllowException("不同客户的业务单不能一起操作，单号："
									+ b.getBillCode());
						}

						if (b.getTallySetId() != form.getAdjustBill()
								.getTallySetId()) {
							throw new NotAllowException("业务单帐套与当前开单帐套不一致，单号："
									+ b.getBillCode());
						}
					}

					if (doActs[1].equals("Y")) {
						iList = adjustBillItemDao.getList(form.getAdjustBill()
								.getRelType(), b.getId(), false);
						if (iList.size() > 0) {
							be.setHasAdjust("Y");
						} else {
							be.setHasAdjust("N");
						}
					} else if (doActs[2].equals("Y")) {
						be.setHasAdjust("Y");
					}

					if (updateRelBillCost && doActs[0].equals("Y")
							&& doActs[1].equals("N")) {
						if (form.getAdjustBill().getAdjustType()
								.equals("COLLECT_ADD")) {
							be.setAdjustCost(be.getAdjustCost().add(
									bean1.getAdjustBillItem().getThisCost()));
						} else {
							be.setAdjustCost(be.getAdjustCost().subtract(
									bean1.getAdjustBillItem().getThisCost()));
						}

						if (b.getCost().subtract(be.getCollectedCost())
								.add(be.getAdjustCost())
								.compareTo(new BigDecimal(0)) == 0) {
							be.setCollected("Y");
						} else {
							be.setCollected("N");
						}
					}

					adjustBillDao.update(be);
					if (updateRelBillCost) {
						BusinessProfit arg23 = businessBillDao
								.getBusinessProfit(b.getBillCode());
						if (arg23 == null) {
							arg23 = new BusinessProfit();
							arg23.setBbCode(b.getBillCode());
							arg23.setId(idGenerator.getId(BusinessProfit.class
									.getName()));
							arg23.setCooperCost(new BigDecimal(0));
							arg23.setFeeCost(new BigDecimal(0));
							arg23.setMaterialCost(new BigDecimal(0));
							arg23.setProfitCost(b.getCost().add(
									be.getAdjustCost()));
							businessBillDao.save(arg23);
						} else {
							arg23.setProfitCost(b.getCost()
									.add(be.getAdjustCost())
									.subtract(arg23.getCooperCost())
									.subtract(arg23.getFeeCost())
									.subtract(arg23.getMaterialCost()));
							businessBillDao.update(arg23);
						}
					}
				} else if (form.getAdjustBill().getRelType()
						.equals("PURCHASE_BILL")) {
					PurchaseBill arg19 = (PurchaseBill) adjustBillDao.get(
							PurchaseBill.class, bean1.getAdjustBillItem()
									.getBusinessBillId());
					PurchaseBillExt arg21 = (PurchaseBillExt) adjustBillDao
							.get(PurchaseBillExt.class, bean1
									.getAdjustBillItem().getBusinessBillId());
					if (arg19 == null) {
						throw new NotAllowException("找不到id为"
								+ bean1.getAdjustBillItem().getBusinessBillId()
								+ "的单据");
					}

					doActs = this.saveItem(form, bean1, adjustBillItemDao,
							arg18);
					if (bean1.getStatus().equals("NEW")
							&& bean1.getAdjustBillItem().getThisCost()
									.compareTo(new BigDecimal(0)) == 1) {
						++arg18;
					}

					if (doActs[0].equals("Y")) {
						if (arg21.getExamined().equals("N")) {
							throw new NotAllowException("单号为"
									+ arg19.getBillCode() + "的单据还没审核，不能操作");
						}

						if (arg21.getEnabled().equals("N")) {
							throw new NotAllowException("单号为"
									+ arg19.getBillCode() + "的单据已作废，不能操作");
						}

						if (arg19.getProviderId() != form.getAdjustBill()
								.getClientId()) {
							throw new NotAllowException("不同供应商的单据不能一起操作，单号："
									+ arg19.getBillCode());
						}

						if (arg19.getTallySetId() != form.getAdjustBill()
								.getTallySetId()) {
							throw new NotAllowException("单据帐套与当前开单帐套不一致，单号："
									+ arg19.getBillCode());
						}
					}

					if (doActs[1].equals("Y")) {
						iList = adjustBillItemDao.getList(form.getAdjustBill()
								.getRelType(), arg19.getId(), false);
						if (iList.size() > 0) {
							arg21.setHasAdjust("Y");
						} else {
							arg21.setHasAdjust("N");
						}
					} else if (doActs[2].equals("Y")) {
						arg21.setHasAdjust("Y");
					}

					if (updateRelBillCost && doActs[0].equals("Y")
							&& doActs[1].equals("N")) {
						if (!form.getAdjustBill().getAdjustType()
								.equals("PAY_ADD")
								&& !form.getAdjustBill().getAdjustType()
										.equals("PUR_BACK_ADD")) {
							arg21.setAdjustCost(arg21.getAdjustCost().subtract(
									bean1.getAdjustBillItem().getThisCost()));
						} else {
							arg21.setAdjustCost(arg21.getAdjustCost().add(
									bean1.getAdjustBillItem().getThisCost()));
						}

						if (arg19.getCost().subtract(arg21.getPaiedCost())
								.add(arg21.getAdjustCost())
								.compareTo(new BigDecimal(0)) == 0) {
							arg21.setPaied("Y");
						} else {
							arg21.setPaied("N");
						}
					}

					adjustBillDao.update(arg21);
				} else {
					if (!form.getAdjustBill().getRelType()
							.equals("COOPER_BILL")) {
						throw new NotAllowException(
								"adjust type not support yet.");
					}

					CooperBill arg20 = (CooperBill) adjustBillDao.get(
							CooperBill.class, bean1.getAdjustBillItem()
									.getBusinessBillId());
					CooperBillExt arg22 = (CooperBillExt) adjustBillDao.get(
							CooperBillExt.class, bean1.getAdjustBillItem()
									.getBusinessBillId());
					if (arg20 == null) {
						throw new NotAllowException("找不到id为"
								+ bean1.getAdjustBillItem().getBusinessBillId()
								+ "的单据");
					}

					doActs = this.saveItem(form, bean1, adjustBillItemDao,
							arg18);
					if (bean1.getStatus().equals("NEW")
							&& bean1.getAdjustBillItem().getThisCost()
									.compareTo(new BigDecimal(0)) == 1) {
						++arg18;
					}

					if (doActs[0].equals("Y")) {
						if (arg22.getExamined().equals("N")) {
							throw new NotAllowException("单号为"
									+ arg20.getBillCode() + "的单据还没审核，不能操作");
						}

						if (arg22.getEnabled().equals("N")) {
							throw new NotAllowException("单号为"
									+ arg20.getBillCode() + "的单据已作废，不能操作");
						}

						if (arg20.getCooperatorId() != form.getAdjustBill()
								.getClientId()) {
							throw new NotAllowException("不同外协商的单据不能一起操作，单号："
									+ arg20.getBillCode());
						}

						if (arg20.getTallySetId() != form.getAdjustBill()
								.getTallySetId()) {
							throw new NotAllowException("单据帐套与当前开单帐套不一致，单号："
									+ arg20.getBillCode());
						}
					}

					if (doActs[1].equals("Y")) {
						iList = adjustBillItemDao.getList(form.getAdjustBill()
								.getRelType(), arg20.getId(), false);
						if (iList.size() > 0) {
							arg22.setHasAdjust("Y");
						} else {
							arg22.setHasAdjust("N");
						}
					} else if (doActs[2].equals("Y")) {
						arg22.setHasAdjust("Y");
					}

					if (updateRelBillCost && doActs[0].equals("Y")
							&& doActs[1].equals("N")) {
						if (form.getAdjustBill().getAdjustType()
								.equals("COO_ADD")) {
							arg22.setAdjustCost(arg22.getAdjustCost().add(
									bean1.getAdjustBillItem().getThisCost()));
						} else {
							arg22.setAdjustCost(arg22.getAdjustCost().subtract(
									bean1.getAdjustBillItem().getThisCost()));
						}

						if (arg20.getCost().subtract(arg22.getPaiedCost())
								.add(arg22.getAdjustCost())
								.compareTo(new BigDecimal(0)) == 0) {
							arg22.setPaied("Y");
						} else {
							arg22.setPaied("N");
						}
					}

					adjustBillDao.update(arg22);
				}
			}
		}

	}

	private String[] saveItem(AdjustBillBean form, AdjustBillItemBean bean,
			AdjustBillItemDao adjustBillItemDao, long newItemId) {
		String doDb = "N";
		String doDel = "N";
		String doIns = "N";
		bean.getAdjustBillItem().setBillId(form.getAdjustBill().getId());
		if (bean.getStatus().equals("DELETE")) {
			if (bean.getOldId() != 0L) {
				adjustBillItemDao.delete(bean.getOldId());
				doDb = "Y";
				doDel = "Y";
			}
		} else if (bean.getStatus().equals("EDIT")) {
			if (bean.getAdjustBillItem().getThisCost()
					.compareTo(new BigDecimal(0)) != 0) {
				adjustBillItemDao.update(bean.getAdjustBillItem());
				doDb = "Y";
			} else {
				adjustBillItemDao.delete(bean.getOldId());
				doDb = "Y";
				doDel = "Y";
			}
		} else if (bean.getStatus().equals("NEW")) {
			if (bean.getAdjustBillItem().getThisCost()
					.compareTo(new BigDecimal(0)) == 1) {
				bean.getAdjustBillItem().setId(newItemId);
				adjustBillItemDao.save(bean.getAdjustBillItem());
				doDb = "Y";
				doIns = "Y";
			}
		} else if (bean.getStatus().equals("UNCHANGE")) {
			doDb = "Y";
		}

		return new String[]{doDb, doDel, doIns};
	}

	public void txExamine(long id, AdjustBillDao adjustBillDao,
			AdjustBillItemDao adjustBillItemDao, ReceiveBillDao receiveBillDao,
			PayBillDao payBillDao, BusinessBillDao businessBillDao,
			IdGenerator idGenerator, long examinerId, String examinerName)
			throws Exception {
		AdjustBill a = (AdjustBill) adjustBillDao.get(AdjustBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("N")) {
			List itemList = adjustBillItemDao.getListByAdjustBill(a.getId());
			a.setExamined("Y");
			a.setExamSms("N");
			a.setExamineTime(new Date());
			a.setExaminerId(examinerId);
			a.setExaminerName(examinerName);
			a.setUpdateTime(new Date());
			adjustBillDao.update(a);
			this.updateRelBill(a.getRelType(), a.getAdjustType(), itemList,
					adjustBillDao, receiveBillDao, payBillDao,
					adjustBillItemDao, businessBillDao, idGenerator, "EXA",
					"ADD", true);
			this.insertFinanceDetail(a, adjustBillDao, idGenerator);
		}

	}

	public void txUnExamine(long id, AdjustBillDao adjustBillDao,
			AdjustBillItemDao adjustBillItemDao, ReceiveBillDao receiveBillDao,
			PayBillDao payBillDao, BusinessBillDao businessBillDao,
			IdGenerator idGenerator, FinanceDetailDao financeDetailDao)
			throws Exception {
		AdjustBill a = (AdjustBill) adjustBillDao.get(AdjustBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("Y")) {
			a.setExamined("N");
			a.setExamSms("Y");
			a.setExaminerId(0L);
			a.setExaminerName("");
			a.setUpdateTime(new Date());
			adjustBillDao.update(a);
			this.updateRelBill(a.getRelType(), a.getAdjustType(),
					adjustBillItemDao.getListByAdjustBill(id), adjustBillDao,
					receiveBillDao, payBillDao, adjustBillItemDao,
					businessBillDao, idGenerator, "UN_EXA", "SUB", true);
			financeDetailDao.delete(a.getId(), "ADJUST_BILL");
		}

	}

	public void txCancel(long id, String reason, AdjustBillDao adjustBillDao,
			AdjustBillItemDao adjustBillItemDao, ReceiveBillDao receiveBillDao,
			PayBillDao payBillDao, BusinessBillDao businessBillDao,
			IdGenerator idGenerator, UserBean ub,
			FinanceDetailDao financeDetailDao) throws Exception {
		AdjustBill d = (AdjustBill) adjustBillDao.get(AdjustBill.class, id);
		if (d != null && d.getEnabled().equals("Y")) {
			d.setEnabled("N");
			d.setExamSms("N");
			d.setDisableManId(ub.getUser().getStafferId());
			d.setUpdateTime(new Date());
			adjustBillDao.update(d);
			List itemList = adjustBillItemDao.getListByAdjustBill(id);
			boolean checkBbStatus = false;
			String costType = "NONE";
			if (d.getExamined().equals("Y")) {
				checkBbStatus = true;
				costType = "SUB";
			}

			this.updateRelBill(d.getRelType(), d.getAdjustType(), itemList,
					adjustBillDao, receiveBillDao, payBillDao,
					adjustBillItemDao, businessBillDao, idGenerator, "CAN",
					costType, checkBbStatus);
			if (d.getExamined().equals("Y")) {
				financeDetailDao.delete(d.getId(), "ADJUST_BILL");
			}

			SystemLog log = new SystemLog();
			log.setId(idGenerator.getId(SystemLog.class.getName()));
			log.setActionCode("ADJUST_BILL");
			log.setLogType("CANCEL_LOG");
			log.setCreaterId(ub.getStaffer().getId());
			log.setCreaterName(ub.getStaffer().getStafferName());
			log.setDepName(ub.getDepName());
			log.setDepId(ub.getStaffer().getDepartmentId());
			log.setStatus("S");
			log.setUserName(ub.getUser().getUserName());
			log.setIp(d.getBillCode());
			log.setMark("");
			log.setRelId(id);
			log.setCancelReason(reason);
			log.setTallySetId(d.getTallySetId());
			adjustBillDao.save(log);
		}

	}

	private void updateRelBill(String relType, String adjType,
			List<?> itemList, AdjustBillDao adjustBillDao,
			ReceiveBillDao receiveBillDao, PayBillDao payBillDao,
			AdjustBillItemDao adjustBillItemDao,
			BusinessBillDao businessBillDao, IdGenerator idGenerator,
			String updType, String costType, boolean checkBbStatus)
			throws Exception {
		for (int i = 0; i < itemList.size(); ++i) {
			AdjustBillItem item = (AdjustBillItem) itemList.get(i);
			List iList;
			if (relType.equals("BUSINESS_BILL")) {
				BusinessBill arg18 = (BusinessBill) businessBillDao.get(
						BusinessBill.class, item.getBusinessBillId());
				BusinessBillExt arg20 = (BusinessBillExt) businessBillDao.get(
						BusinessBillExt.class, item.getBusinessBillId());
				if (arg18 == null) {
					throw new NotAllowException("找不到id为"
							+ item.getBusinessBillId() + "的业务单");
				}

				if (checkBbStatus) {
					if (arg20.getExamined().equals("N")) {
						throw new NotAllowException("单号为" + arg18.getBillCode()
								+ "的业务单还没审核，不能操作!");
					}

					if (arg20.getEnabled().equals("N")) {
						throw new NotAllowException("单号为" + arg18.getBillCode()
								+ "的业务单已经作废，不能操作!");
					}

					if (arg20.getClosed().equals("Y")) {
						throw new NotAllowException("单号为" + arg18.getBillCode()
								+ "的业务单已经结清，不能操作!");
					}
				}

				if (updType.equals("CAN")) {
					iList = adjustBillItemDao.getList(relType, arg18.getId(),
							false);
					if (iList.size() > 0) {
						arg20.setHasAdjust("Y");
					} else {
						arg20.setHasAdjust("N");
					}
				}

				if (!costType.equals("NONE")) {
					if (costType.equals("ADD")) {
						if (adjType.equals("COLLECT_ADD")) {
							arg20.setAdjustCost(arg20.getAdjustCost().add(
									item.getThisCost()));
						} else {
							arg20.setAdjustCost(arg20.getAdjustCost().subtract(
									item.getThisCost()));
						}
					} else if (adjType.equals("COLLECT_ADD")) {
						arg20.setAdjustCost(arg20.getAdjustCost().subtract(
								item.getThisCost()));
					} else {
						arg20.setAdjustCost(arg20.getAdjustCost().add(
								item.getThisCost()));
					}

					if (arg18.getCost().subtract(arg20.getCollectedCost())
							.add(arg20.getAdjustCost())
							.compareTo(new BigDecimal(0)) == 0) {
						arg20.setCollected("Y");
					} else {
						arg20.setCollected("N");
					}
				}

				adjustBillDao.update(arg20);
				BusinessProfit arg21 = businessBillDao.getBusinessProfit(arg18
						.getBillCode());
				if (arg21 == null) {
					arg21 = new BusinessProfit();
					arg21.setBbCode(arg18.getBillCode());
					arg21.setId(idGenerator.getId(BusinessProfit.class
							.getName()));
					arg21.setCooperCost(new BigDecimal(0));
					arg21.setFeeCost(new BigDecimal(0));
					arg21.setMaterialCost(new BigDecimal(0));
					arg21.setProfitCost(arg18.getCost().add(
							arg20.getAdjustCost()));
					businessBillDao.save(arg21);
				} else {
					arg21.setProfitCost(arg18.getCost()
							.add(arg20.getAdjustCost())
							.subtract(arg21.getCooperCost())
							.subtract(arg21.getFeeCost())
							.subtract(arg21.getMaterialCost()));
					businessBillDao.update(arg21);
				}
			} else if (relType.equals("PURCHASE_BILL")) {
				PurchaseBill arg17 = (PurchaseBill) adjustBillDao.get(
						PurchaseBill.class, item.getBusinessBillId());
				PurchaseBillExt arg19 = (PurchaseBillExt) adjustBillDao.get(
						PurchaseBillExt.class, item.getBusinessBillId());
				if (arg17 == null) {
					throw new NotAllowException("找不到id为"
							+ item.getBusinessBillId() + "的采购单");
				}

				if (checkBbStatus) {
					if (arg19.getExamined().equals("N")) {
						throw new NotAllowException("单号为" + arg17.getBillCode()
								+ "的采购单还没审核，不能操作!");
					}

					if (arg19.getEnabled().equals("N")) {
						throw new NotAllowException("单号为" + arg17.getBillCode()
								+ "的采购单已经作废，不能操作!");
					}
				}

				if (updType.equals("CAN")) {
					iList = adjustBillItemDao.getList(relType, arg17.getId(),
							false);
					if (iList.size() > 0) {
						arg19.setHasAdjust("Y");
					} else {
						arg19.setHasAdjust("N");
					}
				}

				if (!costType.equals("NONE")) {
					if (costType.equals("ADD")) {
						if (!adjType.equals("PAY_ADD")
								&& !adjType.equals("PUR_BACK_ADD")) {
							arg19.setAdjustCost(arg19.getAdjustCost().subtract(
									item.getThisCost()));
						} else {
							arg19.setAdjustCost(arg19.getAdjustCost().add(
									item.getThisCost()));
						}
					} else if (!adjType.equals("PAY_ADD")
							&& !adjType.equals("PUR_BACK_ADD")) {
						arg19.setAdjustCost(arg19.getAdjustCost().add(
								item.getThisCost()));
					} else {
						arg19.setAdjustCost(arg19.getAdjustCost().subtract(
								item.getThisCost()));
					}

					if (arg17.getCost().subtract(arg19.getPaiedCost())
							.add(arg19.getAdjustCost())
							.compareTo(new BigDecimal(0)) == 0) {
						arg19.setPaied("Y");
					} else {
						arg19.setPaied("N");
					}
				}

				adjustBillDao.update(arg19);
			} else {
				if (!relType.equals("COOPER_BILL")) {
					throw new Exception("adjust type not support yet.");
				}

				CooperBill b = (CooperBill) adjustBillDao.get(CooperBill.class,
						item.getBusinessBillId());
				CooperBillExt be = (CooperBillExt) adjustBillDao.get(
						CooperBillExt.class, item.getBusinessBillId());
				if (b == null) {
					throw new NotAllowException("找不到id为"
							+ item.getBusinessBillId() + "的外协单");
				}

				if (checkBbStatus) {
					if (be.getExamined().equals("N")) {
						throw new NotAllowException("单号为" + b.getBillCode()
								+ "的外协单还没审核，不能操作!");
					}

					if (be.getEnabled().equals("N")) {
						throw new NotAllowException("单号为" + b.getBillCode()
								+ "的外协单已经作废，不能操作!");
					}
				}

				if (updType.equals("CAN")) {
					iList = adjustBillItemDao
							.getList(relType, b.getId(), false);
					if (iList.size() > 0) {
						be.setHasAdjust("Y");
					} else {
						be.setHasAdjust("N");
					}
				}

				if (!costType.equals("NONE")) {
					if (costType.equals("ADD")) {
						if (adjType.equals("COO_ADD")) {
							be.setAdjustCost(be.getAdjustCost().add(
									item.getThisCost()));
						} else {
							be.setAdjustCost(be.getAdjustCost().subtract(
									item.getThisCost()));
						}
					} else if (adjType.equals("COO_ADD")) {
						be.setAdjustCost(be.getAdjustCost().subtract(
								item.getThisCost()));
					} else {
						be.setAdjustCost(be.getAdjustCost().add(
								item.getThisCost()));
					}

					if (b.getCost().subtract(be.getPaiedCost())
							.add(be.getAdjustCost())
							.compareTo(new BigDecimal(0)) == 0) {
						be.setPaied("Y");
					} else {
						be.setPaied("N");
					}
				}

				adjustBillDao.update(be);
			}
		}

	}
}